﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Entities;
using DLTN.Data;
using System.Data;

public partial class Admin_DanhMuc_DMVeMayBay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindNoiDi(cboDiemDi);
            BindCatalog.BindNoiDen(cboDiemDen);
            BindCatalog.BindHangMayBay(cboHangMayBay);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsBookCategories = DataRepository.Provider.ExecuteDataSet("VeMayBay_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsBookCategories.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsBookCategories.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;

    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMVeMayBay.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            VeMayBay ent_VeMayBay = new VeMayBay();
            ent_VeMayBay.MaVe = txtMaVe.Text;
            ent_VeMayBay.IdDiemDi = Convert.ToInt32(cboDiemDi.SelectedValue);
            ent_VeMayBay.IdDiemDen = Convert.ToInt32(cboDiemDen.SelectedValue);
            ent_VeMayBay.QuaCanh =Common.StandardString(txtQuaCanh.Text);
            ent_VeMayBay.IdHangMayBay = Convert.ToInt32(cboHangMayBay.SelectedValue);
            ent_VeMayBay.Gia = Convert.ToInt32(txtGia.Text);
            ent_VeMayBay.NgayCapNhat = Common.GetServerDateTime();
            ent_VeMayBay.TrangThai = chkTrangThai.Checked;

            if (ent_VeMayBay.MaVe != null)
            {
                
                updateStatus = DataRepository.VeMayBayProvider.Insert(ent_VeMayBay);
            }
            else
            {
                updateStatus = DataRepository.VeMayBayProvider.Update(ent_VeMayBay);
            }
            if (updateStatus)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.VEMAYBAY_CAPNHAT_THANHCONG, true);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.VEMAYBAY_CAPNHAT_THATBAI, true);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMVeMayBay.aspx.cs", "cmdSave_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.VEMAYBAY_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtMaVe.Text = "";
        cboDiemDi.SelectedValue = "0";
        cboDiemDen.SelectedValue = "0";
        txtQuaCanh.Text = "";
        cboHangMayBay.SelectedValue = "0";
        txtGia.Text = "";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                string MaVe = e.CommandArgument.ToString();

                VeMayBay ent_VeMayBay = DataRepository.VeMayBayProvider.GetByMaVe(MaVe);
                if (ent_VeMayBay != null)
                {
                    txtMaVe.Text = ent_VeMayBay.MaVe;
                    cboDiemDi.SelectedValue = ent_VeMayBay.IdDiemDi.ToString();
                    cboDiemDen.SelectedValue = ent_VeMayBay.IdDiemDen.ToString();
                    txtQuaCanh.Text = ent_VeMayBay.QuaCanh;
                    cboHangMayBay.SelectedValue = ent_VeMayBay.IdHangMayBay.ToString();
                    txtGia.Text = ent_VeMayBay.Gia.ToString();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMVeMayBay.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                string MaVe = e.CommandArgument.ToString();
                bool deleteStatus = DataRepository.VeMayBayProvider.Delete(MaVe);
                if (!deleteStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.VEMAYBAY_XOA_THATBAI, true);
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.VEMAYBAY_XOA_THANHCONG, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMVeMayBay.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.VEMAYBAY_XOA_THATBAI, true);
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
        txtMaVe.Text = "";
        cboDiemDi.SelectedValue = "0";
        cboDiemDen.SelectedValue = "0";
        txtQuaCanh.Text = "";
        cboHangMayBay.SelectedValue = "0";
        txtGia.Text = "";
    }

    public string GetDiemDi(object Char)
    {
        int idDiemDi = Convert.ToInt32(Char.ToString());
        using (NoiDi ent_NoiDi = DataRepository.NoiDiProvider.GetById(idDiemDi))
        {
            return ent_NoiDi.TenNoiDi;
        }
    }
    public string GetDiemDen(object Char)
    {
        int idDiemDen = Convert.ToInt32(Char.ToString());
        using (NoiDen ent_NoiDen = DataRepository.NoiDenProvider.GetById(idDiemDen))
        {
            return ent_NoiDen.TenNoiDen;
        }
    }
    public string GetHangMayBay(object Char)
    {
        int idHangMaybay = Convert.ToInt32(Char.ToString());
        using (HangMayBay ent_HangMayBay = DataRepository.HangMayBayProvider.GetById(idHangMaybay))
        {
            return ent_HangMayBay.TenHangMayBay;
        }
    }
}